package SystemeB;

import java.io.PipedReader;
import java.io.PipedWriter;

/**************************************************************************************
 ** Class name: SortFilter 
 *	Original author: Roslane Moussouni, XXX, XXX
 *	CMU Date: 17/01/2011
 * 	Version 1.0
 ** 
 *************************************************************************************** 
 ** Purpose: Assignment 1 for LOG430, Architecture logicielle. This assignment is
 * designed to illustrate a pipe and filter architecture. For the instructions,
 * refer to the assignment write-up.
 ** 
 ** Abstract: This class is intended to be a filter that will collect the streams
 * from two input pipes and merge them into one output pipe..
 ** 
 ** Pseudo Code:
 ** 
 ** connect to input pipe 1 connect to input pipe 2 connect to output pipe
 ** 
 ** while not done read char1 from input pipe 1 read char2 from input pipe 2
 * string1 = string1 + char1 string2 = string2 + char2 write string1 to output
 * pipe write string2 to output pipe end while
 ** 
 ** close pipes close file
 ** 
 ** Running the program
 ** 
 ** See MainFilter.java
 ** 
 ** Modification Log
 ************************************************************************************** 
 ** 
 **************************************************************************************/

public class KeepStudentInfoFilter extends Thread {
	// Declarations

	// Create local pipes to that will connect to upstream filters
	PipedReader InputPipe1 = new PipedReader();

	// Create local pipes to that will connect to downstream filter
	PipedWriter OutputPipe1 = new PipedWriter();

	public KeepStudentInfoFilter(PipedWriter InputPipe1, PipedWriter OutputPipe1) {
		try {
			// Connect InputPipes to upstream filters
			this.InputPipe1.connect(InputPipe1);
			System.out.println("KeepStudentInfoFilter:: connected to upstream filters.");
		} catch (Exception Error) {
			System.out.println("KeepStudentInfoFilter:: Error connecting input pipes.");
		} // try/catch

		try {
			// Connect OutputPipe1 to downstream filter
			this.OutputPipe1 = OutputPipe1;
			System.out.println("KeepStudentInfoFilter:: connected to downstream filter.");
		} catch (Exception Error) {
			System.out.println("KeepStudentInfoFilter:: Error connecting output pipe.");
		} // catch

	} // Constructor

	// This is the method that is called when the thread is started in
	// MainFilter

	public void run() {
		// Declarations
		boolean isDone; // Flags for reading from each pipe

		// Begin process data from the input pipes
		try {
			// Declarations

			// Need to be an array for easy conversion to string
			char[] CharacterValue1 = new char[1];

			// Indicate when you are done reading on pipes #1 and #2
			isDone = false;

			// integer values read from the pipes
			int IntegerCharacter1;

			// lines of text from input pipes #1 and #2
			String LineOfText = "";

			// Indicate whether lines of text are ready to be output
			// to downstream filters
			boolean isWriting = false;

			// Main loop for reading data

			while (!isDone) {
				// Read pipe #1
				if (!isDone) {
					IntegerCharacter1 = InputPipe1.read();
					CharacterValue1[0] = (char) IntegerCharacter1;

					if (IntegerCharacter1 == -1) // pipe #1 is closed
					{
						isDone = true;
						System.out
								.println("KeepStudentInfoFilter:: Input pipe 1 closed.");

						try {
							InputPipe1.close();
						} catch (Exception Error) {
							System.out
									.println("KeepStudentInfoFilter:: Error closing input pipe 1.");
						} // try/catch

					} else {

						if (IntegerCharacter1 == '\n') // end of line
						{
							System.out.println("KeepStudentInfoFilter:: Received: "
									+ LineOfText + " on input pipe 1.");			
							
							isWriting = true;
							
							// Traitement sur la ligne recue
							String[] tmp = LineOfText.split(" ");
							LineOfText = tmp[0] + " " + tmp[1] + " " + tmp[2];
							
						} else {
							LineOfText += new String(CharacterValue1);
						} // if

					} // if ( IntegerCharacter1 == -1 )

				} // if (!Done1)

				if (isWriting) {
					isWriting = false;

					try {
						System.out.println("KeepStudentInfoFilter:: Sending "
								+ LineOfText + " to output pipe.");
						LineOfText += "\n";
						OutputPipe1.write(LineOfText, 0, LineOfText.length());
						OutputPipe1.flush();
					} catch (Exception IOError) {
						System.out.println("KeepStudentInfoFilter:: Write Error.");
					} // try/catch

					LineOfText = "";

				} // if ( isWriting )

			} // while ( !isDone )

		} // try

		catch (Exception Error) {
			System.out.println("KeepStudentInfoFilter:: Interrupted.");
		} // catch

		try {
			System.out.println("KeepStudentInfoFilter:: output pipe closed.");
			OutputPipe1.close();
		} catch (Exception Error) {
			System.out.println("KeepStudentInfoFilter:: Error closing pipes");
		} // try/catch

	} // run

} // class
